<?php

header("Content-type: text/html; charset=utf-8");

/**
 * 虚拟主机创建工具
 * 
 * @author anyon <zoujingli@eqwah.com>
 * @date 2014/08/13 01:10:00
 */
class Vhost {

	/**
	 * 当前文件位置
	 * F
	 * @var string 
	 */
	protected $path = null;
	protected $root = null;
	protected $vhostPath = null;

	/**
	 * 构造函数
	 */
	public function __construct() {
		$this->root = '/home/wwwroot';
		$this->path = str_replace('\\', '/', dirname(__FILE__));
		$this->vhostPath = '/usr/local/apache/conf/vhost';
	}

	/**
	 * 创建虚拟主机配置文件
	 *
	 * @param type $app 应用名称
	 */
	public function createVhost($app) {
		$confing_file = "{$this->path}/{$app}.dev.eqway.com.conf";
		if (file_put_contents($confing_file, $this->_getVhostConfig($app))) {
			$this->_echo('--- create vhost ');
			exec("mv {$confing_file} {$this->vhostPath}/", $a, $b);
			if (empty($a)) {
				$this->_echo('success');
				if (!is_dir("{$this->root}/{$app}")) {
					mkdir("{$this->root}/{$app}", 0777, true);
//					$indexFile = "{$this->root}/{$app}/index.php";
//					if (!is_file($indexFile)) {
//						file_put_contents($indexFile, "<?php '<h1>{$app}, It\'s Works!</h1>';");
//					}
				}
				$this->reload($app);
			} else {
				$this->_echo('error');
			}
		}
	}

	/**
	 * 重启apache 需要拓展支持
	 * 
	 * @return boolean
	 */
	public function reload($app) {
		$this->_echo('<br > --- apache reload ');
		exec("sudo chmod u+s {$this->path}/reload");
		exec("{$this->path}/reload", $a, $b);
		if (empty($a)) {
			$this->_echo('success');
			$this->svnup($app);
			return true;
		} else {
			$this->_echo('error');
			return false;
		}
	}

	/**
	 * 更新svb目录
	 * 
	 * @param type $app 应用名称
	 * @param type $uri SVN绝对目录
	 */
	public function svnup($app, $uri = '') {
		$this->_echo('<br > --- svn up ');
		include 'phpsvn.php';
		$phpsvn = new phpsvn();
		if (!is_dir('./temp/')) {
			mkdr('./temp/', 0755, true);
		}
		$phpsvn->setOptions(array(
			'username'	 => 'zoujingli',
			'password'	 => '888888',
			'config-dir' => "{$this->path}/temp/"
		));
		$local = "{$this->root}/{$app}";
		if (!is_dir($local)) {
			mkdir($local, 0755, true);
		}
		if (substr($uri, 0, 3) === 'svn') {
			$phpsvn->setPath($uri, dirname($local));
		} else {
			$phpsvn->setPath("svn://127.0.0.1/01-code/{$app}/", dirname($local));
		}
		$r = $phpsvn->checkout();
		if ($r === false) {
			echo $phpsvn->error();
		} else {
			$this->_echo('success <br/>');
			foreach ($r as $d) {
				if (!empty($d))
					$this->_echo('--- --- ' . $d['v'] . '<br>');
			}
		}
	}

	/**
	 * 虚拟主机的配置文件
	 * 
	 * @param type $name 主机名称
	 * @return string
	 */
	protected function _getVhostConfig($name) {
		return "<VirtualHost *:*>
    ServerName {$name}.dev.eqway.com
    ServerAlias {$name}.dev.eqway.com
    DocumentRoot /home/wwwroot/{$name}
    DirectoryIndex index.php index.html index.htm
    <Directory /home/wwwroot/{$name}>
		Options +Includes -Indexes
        AllowOverride All
        Order Deny,Allow
        Require all granted
        php_admin_value open_basedir /home/wwwroot/{$name}:/tmp:/proc
    </Directory>
    ErrorLog  /home/wwwlog/{$name}_error.log
    TransferLog  /home/wwwlog/{$name}_access.log
</VirtualHost>";
	}

	/**
	 * 实时输出内容
	 * 
	 * @param type $msg
	 */
	protected function _echo($msg) {
		echo $msg;
		ob_flush();
		flush();
	}

}

// http://localhost/doing.php?action=create&appname=APPNAME
// http://localhost/doing.php?action=update&appname=APPNAME&codesvnlink=svn://127.0.0.1/01-code/
if (!empty($_GET['action'])) {
	$vhost = new Vhost();
	if ($_GET['action'] === 'create' && !empty($_GET['appname'])) {
		$vhost->createVhost($_GET['appname']);
	} elseif ($_GET['action'] === 'update' && !empty($_GET['appname'])) {
		$vhost->svnup($_GET['appname'], $_GET['codesvnlink']);
	}
} else {
	echo 'vhost create tools';
}
